package fr.toto.settings.defaultPanels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JDirectories extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -875291147700955640L;
	private DefaultListModel<Path> listModel;
	private String name = " ";
	private JFileChooser fc;
	private JList<Path> list;

	public JDirectories(String title, List<Path> selectedPath) {

		this.name = title;

		listModel = new DefaultListModel<Path>();
		if (selectedPath != null)
			for (Path p : selectedPath) {
				listModel.addElement(p);
			}

		this.setSize(new Dimension(500, 200));
		this.setMinimumSize(new Dimension(200, 100));
		this.setLayout(new BorderLayout(1, 1));

		JLabel lblTitle = new JLabel(this.name);
		this.add(lblTitle, BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setMinimumSize(new Dimension(100, 75));
		this.add(scrollPane, BorderLayout.CENTER);
		list = new JList<Path>(listModel);
		scrollPane.setViewportView(list);

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton addButton = new JButton(" + ");
		addButton.setSize(20, 10);
		addButton.setActionCommand("ADD");
		addButton.addActionListener(this);
		panel.add(addButton);

		JButton subButton = new JButton(" - ");
		subButton.setSize(20, 10);
		subButton.setActionCommand("SUB");
		subButton.addActionListener(this);
		panel.add(subButton);

	}

	public JDirectories(String title) {
		this(title, new ArrayList<Path>());
	}

	public JDirectories() {
		this("Dossiers :", new ArrayList<Path>());
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equalsIgnoreCase("ADD")) {

			fc = new JFileChooser();
			fc.setCurrentDirectory(new java.io.File("."));
			fc.setDialogTitle("Selection du dossier");
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fc.setMultiSelectionEnabled(true);
			fc.setDialogType(JFileChooser.CUSTOM_DIALOG);
			fc.setFileHidingEnabled(true);
			//
			// disable the "All files" option.
			//
			// fc.setAcceptAllFileFilterUsed(false);
			//
			if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				File[] selectedFiles = fc.getSelectedFiles();
				for (int i = 0; i < selectedFiles.length; i++) {
					listModel.addElement(selectedFiles[i].toPath());
				}
			}
		} else {
			List<Path> selectedValues = list.getSelectedValuesList();
			for (Path p : selectedValues) {
				listModel.removeElement(p);
			}

		}
	}

	public List<Path> getList() {
		ArrayList<Path> selectedPath = new ArrayList<Path>();
		for (int i = 0; i < listModel.size(); i++) {
			selectedPath.add(listModel.get(i));
		}
		return selectedPath;

	}

}
